﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace GameName1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        const int SCRWIDTH = 1200;
        const int SCRHEIGHT = 675;
        Texture2D test;

        const bool GAMEPAD = false;
        SpriteFont font;

        Player player1 = new Player(new Vector2(100, 300));

        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            graphics.PreferredBackBufferWidth = SCRWIDTH;
            graphics.PreferredBackBufferHeight = SCRHEIGHT;
            IsFixedTimeStep = true;
            TargetElapsedTime = TimeSpan.FromMilliseconds(8);

            GameObject g = (GameObject)player1;
            base.Initialize();


            Texture2D t = Content.Load<Texture2D>("proj");
            Projectile.texture_array[1] = t;
            t = Content.Load<Texture2D>("proj2");
            Projectile.texture_array[2] = t;


            //font stuff
            font = Content.Load<SpriteFont>("font2");


            //Vector2 v = new Vector2(200.0f, 57.0f);
            //Vector2 v2 = new Vector2(-200.0f, 87.0f);
            //v.Normalize();
            //v2.Normalize();
            //Projectile.proj.Add(new Projectile(new Vector2(0, 0),v, Projectile.PROJ_TYPE.WEP1, ref g));
            //Projectile.proj.Add(new Projectile(new Vector2(120, 0), v, Projectile.PROJ_TYPE.WEP1, ref g));
            //Projectile.proj.Add(new Projectile(new Vector2(1240, 0), v2, Projectile.PROJ_TYPE.WEP1, ref g));
            //Projectile.proj.Add(new Projectile(new Vector2(360, 0), v, Projectile.PROJ_TYPE.WEP1, ref g));

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            test = Content.Load<Texture2D>("test");
            player1.TexHandler.Texture = test;
            //font = Content.Load<SpriteFont>("font");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            Content.Unload();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            GetInput();
            double t = gameTime.ElapsedGameTime.TotalSeconds;
            Projectile.UpdateAll(t);
            player1.Update(t);
            // TODO: Add your update logic here
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            //DRAWCODE STARTS HERE
            //spriteBatch.Draw(player1.Texture,, Color.White);
            Projectile.DrawAll(ref spriteBatch);
            player1.Draw(ref spriteBatch);
            String st="";
            if(Projectile.proj.Count >0)
              st = "Distance: " + Projectile.proj[0].DistanceLeft;
            //spriteBatch.DrawString(font, st, new Vector2(0, 700), Color.White);
            //Console.WriteLine(st + gameTime.ElapsedGameTime);


            //print test
            spriteBatch.DrawString(font, "TESTING TEXT", new Vector2(300, 300), Color.Aqua);

            //DRAWCODE ENDS HERE
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }


        protected void GetInput()
        {
            player1.HasMoved = false;
            if (!GAMEPAD)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                    Exit();

                KeyboardState k = Keyboard.GetState();
                if (k.IsKeyDown(Keys.Right))
                {
                    player1.Move(0.5f);
                    player1.HasMoved = true;

                }
                else if (k.IsKeyDown(Keys.Left))
                {
                    player1.Move(-0.5f);
                    player1.HasMoved = true;

                }

                if (k.IsKeyDown(Keys.Down))
                {
                   // player1.Move(0 , 0.5f);
                }
                else if (k.IsKeyDown(Keys.Up))
                {
                    player1.ActionJump();
                }

                if (k.IsKeyDown(Keys.Space))
                {
                    player1.Shoot(Projectile.PROJ_TYPE.WEP1);
                }

                if (k.IsKeyDown(Keys.LeftControl))
                {
                    player1.Shoot(Projectile.PROJ_TYPE.WEP2);
                }

                if (k.IsKeyDown(Keys.Z))
                {
                    player1.TexHandler.HealthState = TextureHandler.HEALTH_STATE.HURT;
                }

            }
            else
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    Exit();

            }

        }


        
    }
}
